package com.jia.controller;

import com.jia.pojo.Dept;
import com.jia.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    DeptService deptService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/dept/list")
    public List<Dept> list() {
        return deptService.selectAll();
    }
    @GetMapping("/dept/info/{id}")
    public Dept info(@PathVariable Long id) {
        return deptService.selectById(id);
    }
    @PostMapping("/dept/add")
    public int add(Dept dept) {
        return deptService.addDept(dept);
    }
    @PutMapping("dept/update")
    public int update(Dept dept) {
        return deptService.updateDept(dept);
    }
    @DeleteMapping("/dept/delete/{id}")
    public int delete(@PathVariable Long id){
        return deptService.deleteDept(id);
    }

    @GetMapping("/dept/discovery")
    public Object discovery(){
        // 获取微服务列表清单
        List<String> services = discoveryClient.getServices();
        System.out.println("services---"+services);
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getServiceId()
            );
        }
        return this.discoveryClient;
    }
}
